package com.dgty.dg_common.network;

import android.text.TextUtils;


import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.dgty.dg_common.bean.LoginBean;
import com.dgty.dg_common.util.KVUtil;
import com.dgty.dg_common.util.UserUtil;
import com.tencent.mmkv.MMKV;

import java.io.IOException;
import java.util.UUID;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Title: HeadersInterceptor.java
 * Description: Header过滤器
 * Created DateTime: 2023-02-07
 * Created by xxl.
 */
public class HeadersInterceptor implements Interceptor {

    // 重写拦截方法，处理Header信息
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();

        Request.Builder builder = request.newBuilder();

        String timestamp = Long.toString(System.currentTimeMillis());
        LoginBean user = UserUtil.Companion.getSInstance().getUser();
        StringBuffer header = new StringBuffer();
        if (user != null && !TextUtils.isEmpty(user.getAccessToken())) {
            builder.header("token", user.getAccessToken());
            header.append(" token = " + user.getAccessToken());
        }
//        } else {
//            builder.header("token", "1");
//        }
        builder.header("appVersion", AppUtils.getAppVersionName());
        header.append(" appVersion = " + AppUtils.getAppVersionName());
        builder.header("platform", "android");
        header.append(" platform = " + "android");
        builder.header("timestamp", timestamp);
        header.append(" timestamp = " + timestamp);

        Request compressedRequest = builder.build();
        Response response = chain.proceed(compressedRequest);
        Response.Builder responseBuilder = response.newBuilder();

        LogUtils.dTag("okhttp header", header);
        return responseBuilder.build();
    }


}
